﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositorySurvey : ADONetRepositoryBase<Survey> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Survey entity)
        {
            var cmd = new SqlCommand("SurveyInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("entityDate", entity.SurveyDate));
            cmd.Parameters.Add(new SqlParameter("TicketId", entity.Ticket.Id));
            if (entity.SickList != null)
                cmd.Parameters.Add(new SqlParameter("SickListId", entity.SickList.Id));
            else
            {
                cmd.Parameters.Add(new SqlParameter("SickListId", null));
            }
            cmd.Parameters.Add(new SqlParameter("PurposeId", entity.Purpose.Id));
            cmd.Parameters.Add(new SqlParameter("InjuryId", entity.Injury.Id));
            cmd.Parameters.Add(new SqlParameter("ResultId", entity.Result.Id));
            logger.Trace("SqlCommand для записи данных Приема в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("SurveyDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Приема id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Survey entity)
        {
            var cmd = new SqlCommand("SurveyUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("SurveyDate", entity.SurveyDate));
            cmd.Parameters.Add(new SqlParameter("TicketId", entity.Ticket.Id));
            if (entity.SickList != null)
                cmd.Parameters.Add(new SqlParameter("SickListId", entity.SickList.Id));
            else
            {
                cmd.Parameters.Add(new SqlParameter("SickListId", null));
            }
            cmd.Parameters.Add(new SqlParameter("PurposeId", entity.Purpose.Id));
            cmd.Parameters.Add(new SqlParameter("InjuryId", entity.Injury.Id));
            cmd.Parameters.Add(new SqlParameter("ResultId", entity.Result.Id));
            logger.Trace("SqlCommand для обновления данных Приема id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetSurvey", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Приема id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            var cmd = new SqlCommand("GetSurveyList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            logger.Trace("SqlCommand для выборки всех существующих Приемов сформирован");
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetSurveyByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Приема {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Survey GetEntity(SqlDataReader sqlDataReader)
        {
            Survey survey = new Survey();
            IRepository<Ticket> adoNetRepositoryTicket = new ADONetRepositoryTicket();
            IRepository<SickList> adoNetRepositorySickList = new ADONetRepositorySickList();
            IRepository<Purpose> adoNetRepositoryPurpose = new ADONetRepositoryPurpose();
            IRepository<Injury> adoNetRepositoryInjury = new ADONetRepositoryInjury();
            IRepository<Result> adoNetRepositoryResult = new ADONetRepositoryResult();
            survey.Id = (int)sqlDataReader["Id"];
            survey.SurveyDate = (DateTime)sqlDataReader["SurveyDate"];
            survey.Ticket = adoNetRepositoryTicket.GetById((int)sqlDataReader["TicketId"]);
            int sickListId = (int)sqlDataReader["SickListId"];
            if (sickListId != 0)
                survey.SickList = adoNetRepositorySickList.GetById(sickListId);
            survey.Purpose = adoNetRepositoryPurpose.GetById((int)sqlDataReader["PurposeId"]);
            survey.Injury = adoNetRepositoryInjury.GetById((int)sqlDataReader["InjuryId"]);
            survey.Result = adoNetRepositoryResult.GetById((int)sqlDataReader["ResultId"]);
            return survey;
        }
    }
}
